package com.project.service.impl;

import com.project.dto.TicketDTO;
import com.project.service.MoviesService;
import com.project.util.RandomUtil;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @title
 * @author: yuyuan
 * @date: 2020/11/30 16:51
 */
@Service
public class MoviesServiceImpl implements MoviesService {

    private static Map<String, TicketDTO> map = new HashMap<>();

    static {
        map.put("生化危机6",new TicketDTO(1,"生化危机6","34",30));
        map.put("水浒传",new TicketDTO(2,"水浒传","38",35));
        map.put("夺冠",new TicketDTO(3,"夺冠","32",40));
        map.put("你的名字",new TicketDTO(4,"你的名字","35",20));
    }

    @Override
    public synchronized String buyMovieTicket(Integer number, TicketDTO ticketDTO) {
        TicketDTO ticket = map.get(ticketDTO.getTicketName());
        if(ticket.getTicetNum() < number){
            return "购票失败，电影票余量:"+ticket.getTicetNum();
        }
        Integer surplus = ticket.getTicetNum() - number;
        ticket.setTicetNum(surplus);
        map.put(ticketDTO.getTicketName(),ticket);
        String code = RandomUtil.randomPassword();
        Integer price = Integer.parseInt(ticket.getTicketPrice());
        return "购票成功,取票码:"+code+" 总价格:"+number*price+"元";
    }
}
